package com.example.demo.config;


import com.example.demo.filter.CORSFilter;
import com.example.demo.interceptors.JwtInterceptor;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    private JwtInterceptor jwtInterceptor;

    public WebConfig(JwtInterceptor jwtInterceptor) {
        this.jwtInterceptor = jwtInterceptor;
    }

//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        InterceptorRegistration registration = registry.addInterceptor(jwtInterceptor);
//        registration.addPathPatterns("/**");  // 针对所有的请求进行拦截
//        registration.excludePathPatterns("/login"); // 排除掉登录请求
//        registration.excludePathPatterns("/userLogin");
//        registration.excludePathPatterns("/register");
//        registration.excludePathPatterns("/product");
//        registration.excludePathPatterns("/product/{id}");
//
//    }

    @Bean
    public FilterRegistrationBean<CORSFilter> corsFilterFilterRegistrationBean() {
        FilterRegistrationBean<CORSFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new CORSFilter());
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(1);
        return registrationBean;
    }
}
